{
    gROOT->Reset();
    
    // #include <cstdio>
// 	TFile f("out_dkl.root");
	TFile f("../src/out.root");
// 	TFile f("out_old.root");
	
// 	int qdc_choice[] = {1,0}; // { "#Detector" , "#QDC" }
	int qdc_choice[] = {2,3}; // { "#Detector" , "#QDC" }
// 	int qdc_choice[] = {3,1}; // { "#Detector" , "#QDC" }
    
	int tdelta	= 33000;
	int tcenter	= 16500;
	
// 	tdelta	= 1000;
// 	tcenter	= 1250;
	
// 	int tcenter	= tdelta/2;
    int PM_MIN	= qdc_choice[1];
    int ANZ_PM	= 1;
    int DET_MIN	= qdc_choice[0];
	int ANZ_DET	= 1;
	int DeltaTimerCounter = 33;
	int ANZ_PLOTS;
	int DeltaTimeStart = 2;
	int dt = 2;
	int num = 1;
// 	int liste[] = {50,20,10,5,4,3,2};
//     int liste[] = {10,9,8,7,6,5};
//     int liste[] = {90,80,70,50,40,30,20};
    int liste[] = {90,50,20,10,5,3,2};
    int rebinvalue = 2;
// 	int liste[] = {50,5};
	
	// 	TCanvas canvas1;
	TCanvas *canvas1 = new TCanvas("canvas1","canvas1",1);
	canvas1.SetFillColor(10);
	canvas1.SetFrameFillColor(10);
	TLegend *legend = new TLegend(.70,.60,.95,.95,"legend");

	

	ANZ_PLOTS = sizeof(liste)/sizeof(liste[0]);
	char liste_char[ANZ_PLOTS][15];
	
	for (num = 0; num < ANZ_PLOTS; num++) {
		sprintf(liste_char[num], "\\Deltat = %dus", liste[num]);
	}

// 	stringstream sges;
// 	hges = new TH1F("hges","hges",tdelta, (tcenter-tdelta/2), (tcenter+tdelta/2));
// 	sges << "Det" << DET_MIN << "[" << PM_MIN << "] >> " << "hges";
// 	Data->Draw(sges.str().c_str());
// // 	cerr << "axis" << hges->GetYaxis() << endl;
// 	
// 	tcenter = (tcenter-tdelta/2)+hges->GetMaximumBin();
// 	tdelta  = 500;
	
	stringstream smax,smax2;
    smax2 << "DeltaTime == " << liste[0];
    hmax = new TH1F("hmax","hmax",tdelta, (tcenter-tdelta/2), (tcenter+tdelta/2));
	smax << "Det" << DET_MIN << "[" << PM_MIN << "] >> " << "hmax";
	Data->Draw(smax.str().c_str(),smax2.str().c_str());
// 	cerr << "axis" << hmax->GetYaxis() << endl;
	
	tcenter = (tcenter-tdelta/2)+hmax->GetMaximumBin();
// 	tdelta  = 1000;
//     tdelta  = 250;
    tdelta  = 500;
    if (tdelta > tcenter*2)
		tdelta = tcenter*2;

// 	tdelta  = 2 * tcenter;

	
	num = 1;
	TH1F * h[DET_MIN+ANZ_DET][PM_MIN+ANZ_PM][ANZ_PLOTS+DeltaTimeStart];
	

	
for (int k = DeltaTimeStart; k < DeltaTimeStart + (ANZ_PLOTS); k++) {
	for (int j = DET_MIN; j < DET_MIN + (ANZ_DET); j++) {
		for (int i = PM_MIN; i < PM_MIN + (ANZ_PM); i++) {
			stringstream s,s3,title;
			
			s << "h" << j << i << k;
			s3 << "Det" << j << "[" << i << "] >> " << s.str();
			title << "Det" << j << "[" << i << "]";
			cout << s3.str() << endl;

			cout << "hist name:" << s.str() << endl;
// 			cout << "k="<<k<<", j="<<j<<", i="<<i<<endl;
			h[j][i][k] = new TH1F(s.str().c_str(), title.str().c_str(), tdelta, (tcenter-tdelta/2), (tcenter+tdelta/2));
// 			h[j][i][k].Sumw2();			//errorbars//
			h[j][i][k].SetLineColor(i+1-PM_MIN+j-DET_MIN+k-DeltaTimeStart);
	// 		

	
			stringstream s2;
// 			s2 << "DeltaTimerCounter == " << k;
// 			cout << "listenwert="<<liste[k-DeltaTimeStart]<<endl;
            if (DET_MIN > 4)
              s2 << "DeltaTime == " << liste[k-DeltaTimeStart];
            else
              s2 << "DeltaTime == " << liste[k-DeltaTimeStart];
            
            cout << s2.str().c_str() << endl;
            
// 			Data->Draw(s3.str().c_str());
			Data->Draw(s3.str().c_str(), s2.str().c_str());
			
			if (rebinvalue > 1)
				h[j][i][k].Rebin(rebinvalue);
					
			if (k == DeltaTimeStart) {
// 				h[j][i][k]->GetYaxis()->SetRangeUser(0.01, hges->GetMaximum()/80);
// 				h[j][i][k]->GetYaxis()->SetRangeUser(0.01, hmax->GetMaximum()*rebinvalue);
// 				h[j][i][k]->SetStats(kFALSE);
				stringstream legendheader;
				int entries = h[j][i][k]->GetEntries();
				legendheader << entries << " entries " << endl;
// 				legendheader << entries << " entries for h" << j << i << k << endl;
				string legendheader2 = legendheader.str();
// 				legend->SetHeader(legendheader.str().c_str());
				legend->SetHeader(legendheader2.c_str());
				h[j][i][k]->SetStats(kFALSE);
			}
			
			legend->AddEntry(h[j][i][k],liste_char[k - DeltaTimeStart]);
// 			legend->AddEntry(h[j][i][k],"test");

					
		}
	}
}

// // 				h[0]->GetYaxis()->SetRange()
// 	h[PM_MIN]->GetYaxis()->SetRangeUser(0,100);
			
// 	h_all.Draw();
for (int k = DeltaTimeStart; k < DeltaTimeStart + (ANZ_PLOTS); k++) {
	for (int j = DET_MIN; j < DET_MIN + (ANZ_DET); j++) {
		for (int i = PM_MIN; i < PM_MIN + (ANZ_PM); i++) {
          if (i==PM_MIN && j==DET_MIN && k==DeltaTimeStart) {
            h[j][i][k].Draw();
          } else
            h[j][i][k].Draw("same");
		}
	}
}

    
    Double_t par0[3],par1[3],par2[3];
    
//     TF1 *fit0 = new TF1("fit0","gaus");
    TF1 *fit1 = new TF1("fit1","gaus");
    TF1 *fit2 = new TF1("fit2","gaus");
    
//     h10->Fit(fit0,"W");
    h[DET_MIN][PM_MIN][DeltaTimeStart]->Fit(fit1,"W","same");
    h[DET_MIN][PM_MIN][DeltaTimeStart+5]->Fit(fit2,"W","same");
    
//     fit0->GetParameters(&par0[0]);
    fit1->GetParameters(&par1[0]);
    fit2->GetParameters(&par2[0]);
    
    cout << "\n\nDelta E between the 2 gauss " << (int) (par2[1]-par1[1]+0.5) << " CH" << endl;
    
    
    
    legend.SetFillColor(10);
    legend->Draw();
    
// 	h22.Draw();
// 	h23.Draw("same");
// ChangeBackgroundColor(kWhite);
// gClient->NeedRedraw(button);
    
}

